package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@CrossOrigin
@RestController
@RequestMapping("/system/LargeScreen")
public class LargeScreen {

    @Autowired
    private TCkPickingwavegoodsMapper mapper;
    @Autowired
    private TOngoodsshelfMapper ongoodsshelfMapper;

    @Autowired
    private TBaseStorageAreaLocationMapper locationMapper;

    @Autowired
    private ViewTverrMapper tverrMapper;

    @Autowired
    private TMiStockMapper tMiStockMapper;





    //今日出入库任务统计
    @GetMapping("/line")
    public Map<String, Integer> getData() {
        Map<String, Integer> data = new HashMap<>();
        int i = mapper.selectCount();
        int rk = ongoodsshelfMapper.selectCount();

        data.put("outbound", i); // 出库数据
        data.put("inbound", rk);  // 入库数据
        return data;
    }

    @GetMapping("/pe05")
    public Map<String, Integer> pe05() {
        int DAYS_IN_YEAR = 365;
        LocalDate today = LocalDate.now();
        Map<String, Integer> map =new HashMap<>();
        map.put("5天↓", getStockCount(today, -1, 5));
        map.put("5-10天", getStockCount(today, 5, 10));
        map.put("10-20天", getStockCount(today, 10, 20));
        map.put("20-30天", getStockCount(today, 20, 30));
        map.put("30天↑", getStockCount(today, 30, DAYS_IN_YEAR));
        return map;
    }

    private int getStockCount(LocalDate endDate, int startDaysAgo, int endDaysAgo) {
        String DATE_FORMAT = "yyyy-MM-dd";
        TMiStock tMiStock = new TMiStock();
        tMiStock.setEndsTime(endDate.minus(startDaysAgo, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        tMiStock.setStartTime(endDate.minus(endDaysAgo, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        return tMiStockMapper.selectByDays(tMiStock);
    }

    @GetMapping("/pallets")
    public int pallets() {
        int i = locationMapper.selectCount();
        return i;
    }

    @GetMapping("/quantity")
    public int quantity() {
        List<TMiStock> tMiStocks = tMiStockMapper.selectCount();
        if(tMiStocks.size() == 0){
            return tMiStocks.size();
        }else{
            List<TMiStock> collect = tMiStocks.stream().filter(f -> f.getBARCODE().contains("B") && f.getBARCODE().contains("Z")).collect(Collectors.toList());
            return collect.size();

        }

    }

    @GetMapping("/chengpin")
    public int chengpin() {
        List<TMiStock> tMiStocks = tMiStockMapper.selectCount();
        if(tMiStocks.size() == 0){
            return tMiStocks.size();
        }else{
            List<TMiStock> collect = tMiStocks.stream().filter(f -> f.getBARCODE().equals("No Read") || (!f.getBARCODE().contains("B") && !f.getBARCODE().contains("Z"))).collect(Collectors.toList());
            return collect.size();
        }
    }

    @GetMapping("/lines")
    public List getDatas() {

        LocalDate minus = LocalDate.now();
//        LocalDate minus = today.minus(1, ChronoUnit.DAYS);

        Double[] codnCounts = new Double[7];


        IntStream.range(0, 7).forEach(i -> {
            LocalDate startDate = minus.minus(3650, ChronoUnit.DAYS);
            LocalDate endDate = i == 6 ? minus.plusDays(1) : minus.minus(5 - i, ChronoUnit.DAYS);

            codnCounts[i] = getBarcodeCounts(startDate, endDate);

        });

        return Arrays.asList(codnCounts,minus);
    }
    private Double getBarcodeCounts(LocalDate startDate, LocalDate endDate) {
        TMiStock tMiStock = new TMiStock();
        tMiStock.setStartTime(startDate.toString());
        tMiStock.setEndsTime(endDate.toString());
        List<TMiStock> stocks = tMiStockMapper.selectByTime(tMiStock);

        return (double) stocks.size();
    }


    @GetMapping("/kw")
    public Map<String, Object> getPieChartData() {
        Map<String, Object> data = new HashMap<>();
        List<TBaseStorageAreaLocation> getlist = locationMapper.selectTBaseStorageAreaLocationList(new TBaseStorageAreaLocation());

        // 计算各个状态的数量
        long countAvailable = getlist.stream().filter(item -> "0".equals(item.getSTATUS())).count();
        long countInStock = getlist.stream().filter(item -> "2".equals(item.getSTATUS())).count();
        long countFrozen = getlist.stream().filter(item -> "1".equals(item.getSTATUS())).count();

        data.put("total", getlist.size());
        data.put("data", new Object[]{
                new HashMap<String, Object>() {{
                    put("value", countAvailable);
                    put("name", "可入库");
                }},
                new HashMap<String, Object>() {{
                    put("value", countInStock);
                    put("name", "有库存");
                }},
                new HashMap<String, Object>() {{
                    put("value", countFrozen);
                    put("name", "冻结");
                }}
        });
        return data;
    }

    @GetMapping("/peone")
    public Map<String, Object> peone() {
        Map<String, Object> data = new HashMap<>();
        List<TBaseStorageAreaLocation> getlist = locationMapper.selectTBaseStorageAreaLocationList(new TBaseStorageAreaLocation());

        // 计算各个状态的数量
        long countAvailable = getlist.stream().filter(item -> "0".equals(item.getSTATUS())).count();
        long countInStock = getlist.stream().filter(item -> "2".equals(item.getSTATUS())).count();
        long countFrozen = getlist.stream().filter(item -> "1".equals(item.getSTATUS())).count();

        data.put("count", getlist.size());
        data.put("value", countAvailable);

        return data;
    }

    @GetMapping("/petwo")
    public Map<String, Object> petwo() {
        Map<String, Object> data = new HashMap<>();
        List<TBaseStorageAreaLocation> getlist = locationMapper.selectTBaseStorageAreaLocationList(new TBaseStorageAreaLocation());

        // 计算各个状态的数量
        long countAvailable = getlist.stream().filter(item -> "0".equals(item.getSTATUS())).count();
        long countInStock = getlist.stream().filter(item -> "2".equals(item.getSTATUS())).count();
        long countFrozen = getlist.stream().filter(item -> "1".equals(item.getSTATUS())).count();

        data.put("count", getlist.size());
        data.put("value", countInStock);

        return data;
    }

    @GetMapping("/pes")
    public Map<String, Object> pes() {
        Map<String, Object> data = new HashMap<>();
        List<TBaseStorageAreaLocation> getlist = locationMapper.selectTBaseStorageAreaLocationList(new TBaseStorageAreaLocation());

        // 计算各个状态的数量
        long countAvailable = getlist.stream().filter(item -> "0".equals(item.getSTATUS())).count();
        long countInStock = getlist.stream().filter(item -> "2".equals(item.getSTATUS())).count();
        long countFrozen = getlist.stream().filter(item -> "1".equals(item.getSTATUS())).count();

        data.put("count", getlist.size());
        data.put("value", countFrozen);

        return data;
    }

    @GetMapping("/tv")
    public List<TErrorlog> tv() {
        List<TErrorlog> viewTverrData = tverrMapper.getTErrorLogData();
//        ViewTvErr viewTvErr = new ViewTvErr();
//        viewTvErr.setTyp("1");
//        viewTvErr.setTaskI("1");
//        viewTvErr.setError("1");
//        viewTvErr.setDecId("1");

//        viewTverrData.add(viewTvErr);

        return viewTverrData;
    }

    @GetMapping("/task")
    public List<Task> task() {

        List<Task> objects = new ArrayList<>();

        List<TOngoodsshelf> select = ongoodsshelfMapper.select();
        if (!select.isEmpty()){
            for (TOngoodsshelf tOngoodsshelf : select) {
                Task task = new Task();
                task.setTaskGoodsId(tOngoodsshelf.getGOODSID());
                task.setCtl(tOngoodsshelf.getCTL());
                task.setNum(tOngoodsshelf.getShelfNum());
                task.setLocal(tOngoodsshelf.getLocationId());
                if (tOngoodsshelf.getBARCODE().contains("B") && tOngoodsshelf.getBARCODE().contains("Z")) {
                    task.setSpec("半成品");
                }else{
                    task.setSpec("成品");
                }
                task.setType("入库任务");
                objects.add(task);
            }
        }


        List<TCkPickingwavegoods> list = mapper.select();
        if (!list.isEmpty()){
            for (TCkPickingwavegoods tCkPickingwavegoods : list) {
                Task task = new Task();
                task.setTaskGoodsId(tCkPickingwavegoods.getGoodId());
                task.setCtl(tCkPickingwavegoods.getCTL());
                task.setNum(tCkPickingwavegoods.getGoodsNum());
                task.setLocal(tCkPickingwavegoods.getLocId());
                if (tCkPickingwavegoods.getBARCODE().contains("B") && tCkPickingwavegoods.getBARCODE().contains("Z")) {
                    task.setSpec("半成品");
                }else{
                    task.setSpec("成品");
                }
                task.setType("出库任务");
                objects.add(task);
            }
        }
        return objects;
    }

}
